{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "35db18b4-3f20-4090-9402-69ec7c684739",
   "metadata": {},
   "source": [
    "# Bedrock with LangChain using a Prompt that includes Context\n",
    "\n",
    "please refer the detail : https://github.com/aws-samples/amazon-bedrock-workshop/blob/main/01_Generation/02_contextual_generation.ipynb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fe8c40c-20fe-4af0-864e-8e6c828de6b4",
   "metadata": {},
   "source": [
    "## install dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca77056f-30e2-4871-8d34-9574db3ea726",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# dependencies\n",
    "!pip install boto3>=1.28.59\n",
    "!pip install langchain\n",
    "!pip install langchainhub\n",
    "!pip install -U \"anthropic[bedrock]\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65343449-e808-4e95-8085-db8cbf2cda73",
   "metadata": {},
   "source": [
    "## check bedrock availability"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8040cab-2932-4308-9ff7-4bc07ec3b904",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def usage_demo():\n",
    "    \"\"\"\n",
    "    Shows how to list the available foundation models.\n",
    "    This demonstration gets the list of available foundation models and\n",
    "    prints their respective summaries.\n",
    "    \"\"\"\n",
    "    logging.basicConfig(level=logging.INFO)\n",
    "    print(\"-\" * 88)\n",
    "    print(\"Welcome to the Amazon Bedrock demo.\")\n",
    "    print(\"-\" * 88)\n",
    "\n",
    "    bedrock_client = boto3.client(service_name=\"bedrock\", region_name=\"us-east-1\")\n",
    "\n",
    "    wrapper = BedrockWrapper(bedrock_client)\n",
    "\n",
    "    print(\"Listing the available foundation models.\")\n",
    "\n",
    "    try:\n",
    "        for model in wrapper.list_foundation_models():\n",
    "            print_model_details(model)\n",
    "    except ClientError:\n",
    "        logger.exception(\"Couldn't list foundation models.\")\n",
    "        raise\n",
    "\n",
    "    print(\"Getting the details of an individual foundation model.\")\n",
    "\n",
    "    model_id = \"amazon.titan-embed-text-v1\"\n",
    "\n",
    "    try:\n",
    "        print_model_details(wrapper.get_foundation_model(model_id))\n",
    "    except ClientError:\n",
    "        logger.exception(f\"Couldn't get foundation model {model_id}.\")\n",
    "        raise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "197fdf1e-f0cb-4fd1-a89c-6ba7296176da",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import sys\n",
    "import logging\n",
    "import boto3\n",
    "import botocore\n",
    "\n",
    "logger = logging.getLogger(__name__)\n",
    "\n",
    "module_path = \"..\"\n",
    "sys.path.append(os.path.abspath(module_path))\n",
    "from utils import bedrock\n",
    "from utils.BedrockWrapper import BedrockWrapper\n",
    "from utils.GamePlayer import GamePlayer\n",
    "from utils.GameAssistant import GameAssistant\n",
    "from utils.GameMaster import GameMaster\n",
    "from utils.PeTemplates import *\n",
    "from utils import ParseJson, print_ww, Print, Info, Debug, Warn, Error\n",
    "from utils import print_model_details\n",
    "        \n",
    "from botocore.exceptions import ClientError\n",
    "\n",
    "usage_demo()\n",
    "#boto3_bedrock = bedrock.get_bedrock_client()\n",
    "\n",
    "#listModels = bedrock.list_foundation_models(byProvider='meta')\n",
    "\n",
    "# print_ww(\"hello\")\n",
    "# Info(\"hello\")\n",
    "\n",
    "# GA = GameAssistant(template_assistant_role, 1000)\n",
    "# print(GA.agent.prompt.template)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8ca9a03-5f1f-46fc-915a-0fa6d0801335",
   "metadata": {},
   "source": [
    "## Test Part"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "129d5d72-0a98-480f-bca4-c26f0fcd716f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import sys\n",
    "import logging\n",
    "import boto3\n",
    "import botocore\n",
    "\n",
    "logger = logging.getLogger(__name__)\n",
    "\n",
    "module_path = \"..\"\n",
    "sys.path.append(os.path.abspath(module_path))\n",
    "from utils.BedrockRuntimeWrapper import BedrockRuntimeWrapper, invoke\n",
    "from botocore.exceptions import ClientError\n",
    "\n",
    "client = boto3.client(service_name=\"bedrock-runtime\", region_name=\"us-east-1\")\n",
    "wrapper = BedrockRuntimeWrapper(client)\n",
    "\n",
    "mode_id = \"anthropic.claude-3-sonnet-20240229-v1:0\"\n",
    "\n",
    "text_generation_prompt = \"5+3等于多少？\"\n",
    "print(\"\\n====claude3====\\n\")\n",
    "invoke(wrapper, mode_id, text_generation_prompt)\n",
    "print(\"\\n====claude3 with stream response====\\n\")\n",
    "try:\n",
    "    async for completion in wrapper.invoke_claude3_with_response_stream(text_generation_prompt):\n",
    "        print(completion, end=\"\")\n",
    "\n",
    "except ClientError:\n",
    "    logger.exception(\"Couldn't invoke model %s\", model_id)\n",
    "    raise\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22e6c8ee-6c1d-4477-9159-45c870987e44",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import boto3\n",
    "from langchain_community.llms import Bedrock\n",
    "\n",
    "bedrock = boto3.client('bedrock-runtime' , 'us-east-1')\n",
    "\n",
    "MODEL_KWARGS = {\n",
    "\"anthropic.claude-3-sonnet-20240229-v1:0\": {\n",
    "        \"temperature\": 0, \n",
    "        \"top_k\": 250, \n",
    "        \"top_p\": 1, \n",
    "        \"max_tokens_to_sample\": 2**10 \n",
    "}}\n",
    "\n",
    "model_id = 'anthropic.claude-3-sonnet-20240229-v1:0'\n",
    "llm = Bedrock(model_id=model_id, model_kwargs=MODEL_KWARGS[model_id])\n",
    "llm('tell me a joke')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "936436a8-dede-4dce-b10d-a6ff30db8b00",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from anthropic import AnthropicBedrock\n",
    "\n",
    "client = AnthropicBedrock(\n",
    "    # Authenticate by either providing the keys below or use the default AWS credential providers, such as\n",
    "    # using ~/.aws/credentials or the \"AWS_SECRET_ACCESS_KEY\" and \"AWS_ACCESS_KEY_ID\" environment variables.\n",
    "    #aws_access_key=\"<access key>\",\n",
    "    #aws_secret_key=\"<secret key>\",\n",
    "    # Temporary credentials can be used with aws_session_token.\n",
    "    # Read more at https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html.\n",
    "    #aws_session_token=\"<session_token>\",\n",
    "    # aws_region changes the aws region to which the request is made. By default, we read AWS_REGION,\n",
    "    # and if that's not present, we default to us-east-1. Note that we do not read ~/.aws/config for the region.\n",
    "    aws_region=\"us-east-1\",\n",
    ")\n",
    "\n",
    "message = client.messages.create(\n",
    "    model=\"anthropic.claude-3-sonnet-20240229-v1:0\",\n",
    "    max_tokens=256,\n",
    "    messages=[{\"role\": \"user\", \"content\": \"Hello, world\"}]\n",
    ")\n",
    "print(message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0576123a-855f-4322-9770-d8d1504b0898",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n",
    "# SPDX-License-Identifier: Apache-2.0\n",
    "\"\"\"\n",
    "Shows how to generate a message with Anthropic Claude (on demand).\n",
    "\"\"\"\n",
    "import boto3\n",
    "import json\n",
    "import logging\n",
    "\n",
    "from botocore.exceptions import ClientError\n",
    "\n",
    "\n",
    "logger = logging.getLogger(__name__)\n",
    "logging.basicConfig(level=logging.INFO)\n",
    "\n",
    "def generate_message(bedrock_runtime, model_id, system_prompt, messages, max_tokens):\n",
    "\n",
    "    body=json.dumps(\n",
    "        {\n",
    "            \"anthropic_version\": \"bedrock-2023-05-31\",\n",
    "            \"max_tokens\": max_tokens,\n",
    "            \"system\": system_prompt,\n",
    "            \"messages\": messages\n",
    "        }  \n",
    "    )  \n",
    "\n",
    "    response = bedrock_runtime.invoke_model(body=body, modelId=model_id)\n",
    "    response_body = json.loads(response.get('body').read())\n",
    "   \n",
    "    return response_body\n",
    "\n",
    "def main():\n",
    "    \"\"\"\n",
    "    Entrypoint for Anthropic Claude message example.\n",
    "    \"\"\"\n",
    "\n",
    "    try:\n",
    "\n",
    "        bedrock_runtime = boto3.client(service_name='bedrock-runtime')\n",
    "\n",
    "        model_id = 'anthropic.claude-3-sonnet-20240229-v1:0'\n",
    "        system_prompt = \"Please respond.\"\n",
    "        max_tokens = 1000\n",
    "\n",
    "        # Prompt with user turn only.\n",
    "        user_message =  {\"role\": \"user\", \"content\": \"你是谁？\"}\n",
    "        messages = [user_message]\n",
    "\n",
    "        response = generate_message (bedrock_runtime, model_id, system_prompt, messages, max_tokens)\n",
    "        print(\"User turn only.\")\n",
    "        print(json.dumps(response, indent=2))\n",
    "\n",
    "        # Prompt with both user turn and prefilled assistant response.\n",
    "        #Anthropic Claude continues by using the prefilled assistant text.\n",
    "        assistant_message =  {\"role\": \"assistant\", \"content\": \"<emoji>\"}\n",
    "        messages = [user_message, assistant_message]\n",
    "        response = generate_message(bedrock_runtime, model_id,system_prompt, messages, max_tokens)\n",
    "        print(\"User turn and prefilled assistant response.\")\n",
    "        print(json.dumps(response, indent=4))\n",
    "\n",
    "    except ClientError as err:\n",
    "        message=err.response[\"Error\"][\"Message\"]\n",
    "        logger.error(\"A client error occurred: %s\", message)\n",
    "        print(\"A client error occured: \" +\n",
    "            format(message))\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77c9faaf-0fcc-4389-bb51-1d93031b93bf",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from utils.PeTemplates import *\n",
    "from langchain.agents import tool\n",
    "from langchain.output_parsers import PydanticOutputParser\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain_core.pydantic_v1 import BaseModel, Field, validator\n",
    "\n",
    "# Define your desired data structure.\n",
    "class Joke(BaseModel):\n",
    "    setup: str = Field(description=\"question to set up a joke\")\n",
    "    punchline: str = Field(description=\"answer to resolve the joke\")\n",
    "\n",
    "    # You can add custom validation logic easily with Pydantic.\n",
    "    @validator(\"setup\")\n",
    "    def question_ends_with_question_mark(cls, field):\n",
    "        if field[-1] != \"?\":\n",
    "            raise ValueError(\"Badly formed question!\")\n",
    "        return field\n",
    "    \n",
    "# Set up a parser + inject instructions into the prompt template.\n",
    "parser = PydanticOutputParser(pydantic_object=Joke)\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    template=\"Answer the user query.\\n{format_instructions}\\n{query}\\n\",\n",
    "    input_variables=[\"query\"],\n",
    "    partial_variables={\"format_instructions\": parser.get_format_instructions()},\n",
    ")\n",
    "\n",
    "print(prompt)\n",
    "\n",
    "model = claude3_Sonnet\n",
    "# And a query intended to prompt a language model to populate the data structure.\n",
    "prompt_and_model = prompt | model\n",
    "output = prompt_and_model.invoke({\"query\": \"Tell me a joke.\"})\n",
    "parser.invoke(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "898dab35-0dd0-49ee-bf88-b795a73cc607",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from utils.CustomTools import *\n",
    "from utils.PeTemplates import *\n",
    "from langchain.agents import tool\n",
    "\n",
    "# - 夜晚投票(狼人专属行动): WolfVote 参数: target=存活玩家\n",
    "# - 夜晚查验(预言家专属行动): ProphetCheck 参数: target=存活玩家\n",
    "# - 白天怀疑(所有玩家白天可选行动, 非投票): PlayerDoubt 参数: target=存活玩家 \n",
    "# - 白天投票: PlayerVote 参数: target=存活玩家 \n",
    "# - 白天讨论: Debate 参数: content=思考/理由 \n",
    "# - 玩家信息: GetAllPlayersName 参数: 无 \n",
    "# - 死亡遗言: DeathWords 参数: content=给予玩家线索\n",
    "# - 玩家弃权: Pass 参数: 无 \n",
    "# - 其他动作: Pass 参数: 无 \n",
    "\n",
    "@tool\n",
    "def WolfVote(target: str) -> str:\n",
    "    \"\"\"夜晚投票(狼人专属行动)\"\"\"\n",
    "    return \"WolfVote\"\n",
    "\n",
    "@tool\n",
    "def ProphetCheck(target: str) -> str:\n",
    "    \"\"\"夜晚查验(预言家专属行动)\"\"\"\n",
    "    return \"ProphetCheck\"\n",
    "\n",
    "@tool\n",
    "def PlayerDoubt(target: str) -> str:\n",
    "    \"\"\"白天怀疑(所有玩家白天可选行动, 非投票)\"\"\"\n",
    "    return \"PlayerDoubt\"\n",
    "\n",
    "@tool\n",
    "def PlayerVote(target: str) -> str:\n",
    "    \"\"\"白天投票\"\"\"\n",
    "    return \"PlayerVote\"\n",
    "\n",
    "@tool\n",
    "def Debate(target: str) -> str:\n",
    "    \"\"\"白天讨论\"\"\"\n",
    "    return \"Debate\"\n",
    "\n",
    "@tool\n",
    "def GetAllPlayersName(target: str) -> str:\n",
    "    \"\"\"玩家信息\"\"\"\n",
    "    return \"GetAllPlayersName\"\n",
    "\n",
    "@tool\n",
    "def DeathWords(content: str) -> str:\n",
    "    \"\"\"死亡遗言\"\"\"\n",
    "    return \"DeathWords\"\n",
    "\n",
    "@tool\n",
    "def Pass(content: str) -> str:\n",
    "    \"\"\"玩家弃权,说明理由\"\"\"\n",
    "    return \"Pass\"\n",
    "\n",
    " \n",
    "# print(f\"search.name:{search.name}\")\n",
    "# print(f\"search.description:{search.description}\")\n",
    "# print(f\"search.args:{search.args}\")\n",
    "\n",
    "# Define a list of tools\n",
    "tools = [\n",
    "    Tool(\n",
    "        name = \"WolfVote\",\n",
    "        func=WolfVote.run,\n",
    "        description=\"夜晚投票(狼人专属行动)\"\n",
    "    ),\n",
    "    Tool(\n",
    "        name = \"ProphetCheck\",\n",
    "        func=ProphetCheck.run,\n",
    "        description=\"夜晚查验(预言家专属行动)\"\n",
    "    ),\n",
    "    Tool(\n",
    "        name = \"PlayerDoubt\",\n",
    "        func=PlayerDoubt.run,\n",
    "        description=\"白天怀疑(所有玩家白天可选行动, 非投票)\"\n",
    "    ),\n",
    "    Tool(\n",
    "        name = \"PlayerVote\",\n",
    "        func=PlayerVote.run,\n",
    "        description=\"白天投票\"\n",
    "    ),\n",
    "    Tool(\n",
    "        name = \"Pass\",\n",
    "        func=Pass.run,\n",
    "        description=\"玩家弃权,说明理由\"\n",
    "    )\n",
    "]\n",
    "\n",
    "# Using tools, the LLM chain and output_parser to make an agent\n",
    "tool_names = [tool.name for tool in tools]\n",
    "\n",
    "prompt = CustomPromptTemplate(\n",
    "    template=template_werewolf_role.replace(\"{tool_names}\", \",\".join(tool_names)),\n",
    "    tools=tools,\n",
    "    # This omits the `agent_scratchpad`, `tools`, and `tool_names` variables because those are generated dynamically\n",
    "    # This includes the `intermediate_steps` variable because that is needed\n",
    "    input_variables=[\"input\", \"intermediate_steps\", \"history\", 'agent_scratchpad', 'tools', 'tool_names']\n",
    ")\n",
    "print(prompt)\n",
    "# output_parser = CustomOutputParser()\n",
    "\n",
    "llm = claude_instant_llm\n",
    "\n",
    "# LLM chain consisting of the LLM and a prompt\n",
    "# llm_chain = LLMChain(llm=llm, prompt=prompt)\n",
    "\n",
    "agent = create_structured_chat_agent(llm, tools, prompt)\n",
    "\n",
    "# agent = LLMSingleActionAgent(\n",
    "#     llm_chain=llm_chain, \n",
    "#     output_parser=output_parser,\n",
    "#     # We use \"Observation\" as our stop sequence so it will stop when it receives Tool output\n",
    "#     # If you change your prompt template you'll need to adjust this as well\n",
    "#     stop=[\"\\nObservation:\"], \n",
    "#     allowed_tools=tool_names\n",
    "# )\n",
    "\n",
    "# Initiate the agent that will respond to our queries\n",
    "# Set verbose=True to share the CoT reasoning the LLM goes through\n",
    "memory = ConversationBufferWindowMemory(k=2)\n",
    "agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, memory=memory, verbose=False)\n",
    "\n",
    "agent_executor.invoke({\"input\":\"第一个晚上，你支持的玩家该如何行动？\"})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1a82dcee-a14c-4bee-927a-ea230129b227",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:42:17 AM Found credentials from IAM Role: BaseNotebookInstanceEc2InstanceRole\n",
      "04/19/2024 03:42:17 AM Found credentials from IAM Role: BaseNotebookInstanceEc2InstanceRole\n",
      "04/19/2024 03:42:17 AM Found credentials from IAM Role: BaseNotebookInstanceEc2InstanceRole\n",
      "04/19/2024 03:42:17 AM \t===== P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:存活,P8:存活 ResetGame =====\n",
      "04/19/2024 03:42:17 AM P1 is 预言家\n",
      "04/19/2024 03:42:17 AM P2 is 村民\n",
      "04/19/2024 03:42:17 AM P3 is 狼人\n",
      "04/19/2024 03:42:17 AM P4 is 村民\n",
      "04/19/2024 03:42:17 AM P5 is 村民\n",
      "04/19/2024 03:42:17 AM P6 is 狼人\n",
      "04/19/2024 03:42:17 AM P7 is 村民\n",
      "04/19/2024 03:42:17 AM P8 is 村民\n",
      "04/19/2024 03:42:17 AM \t===== P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:存活,P8:存活 RunGame =====\n",
      "04/19/2024 03:42:17 AM \t===== input_tokens: 0 output_tokens 0 ======\n",
      "04/19/2024 03:42:17 AM \t===== elapsed_time: 0.00737452507019043 ======\n",
      "04/19/2024 03:42:17 AM \t===== NIGHT 1 PreAction ======\n",
      "04/19/2024 03:42:17 AM \t===== NIGHT 1 DoAction ======\n",
      "04/19/2024 03:42:17 AM \t\t===== DoMemory NIGHT 1 P1 ======\n",
      "04/19/2024 03:42:17 AM \t\t===== DoReflect NIGHT 1 P1 ======\n",
      "04/19/2024 03:42:17 AM \t\t===== DoAnswer NIGHT 1 P1 ======\n",
      "04/19/2024 03:42:17 AM \tQUESTION: 现在是NIGHT 1,你支持的玩家是P1(预言家身份)..目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:存活,P8:存活(逗号为分割符).现在是第1天夜晚，该如何行动?\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 第一个夜晚所有的行动都是随机的,作为预言家我需要随机选择一个人去查验。\n",
      "\n",
      "{\"action\": \"ProphetCheck\", \"target\": \"P2\"}"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:42:21 AM \t\t===== DoAction NIGHT 1 P1 ======\n",
      "04/19/2024 03:42:21 AM [玩家P1于时间NIGHT 1, 执行动作为:{'action': 'ProphetCheck', 'target': 'P2'}]P2:村民\n",
      "04/19/2024 03:42:21 AM \n",
      " [ROUND ACTION]={'time': 'NIGHT 1', 'player': 'P1', 'status': 1, 'role': '预言家', 'character': '独立思考', 'response': ['{\"action\": \"ProphetCheck\", \"target\": \"P2\"}']}\n",
      "\n",
      "04/19/2024 03:42:21 AM \t\t===== DoMemory NIGHT 1 P3 ======\n",
      "04/19/2024 03:42:21 AM \t\t===== DoReflect NIGHT 1 P3 ======\n",
      "04/19/2024 03:42:21 AM \t\t===== DoAnswer NIGHT 1 P3 ======\n",
      "04/19/2024 03:42:21 AM \tQUESTION: 现在是NIGHT 1,你支持的玩家是P3(狼人身份,本阵营为:P3,P6).目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:存活,P8:存活(逗号为分割符).现在是第1天夜晚，该如何行动?\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 第一天晚上我的行动是随机的,不好判断其他人的身份。我的建议是:\n",
      "\n",
      "{\"action\": \"WolfVote\", \"target\": \"P7\"}\n",
      "\n",
      "第一天晚上随便投票一个人,不要暴露身份。"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:42:30 AM \t\t===== DoAction NIGHT 1 P3 ======\n",
      "04/19/2024 03:42:30 AM \n",
      " [ROUND ACTION]={'time': 'NIGHT 1', 'player': 'P3', 'status': 1, 'role': '狼人', 'character': '组织者', 'response': ['{\"action\": \"WolfVote\", \"target\": \"P7\"}']}\n",
      "\n",
      "04/19/2024 03:42:30 AM \t\t===== DoMemory NIGHT 1 P6 ======\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 当前为第一晚,P3作为狼人投票杀死了P7。"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:42:33 AM \t\t===== DoReflect NIGHT 1 P6 ======\n",
      "04/19/2024 03:42:33 AM \t\t===== DoAnswer NIGHT 1 P6 ======\n",
      "04/19/2024 03:42:33 AM \tQUESTION: 现在是NIGHT 1,你支持的玩家是P6(狼人身份,本阵营为:P3,P6).目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:存活,P8:存活(逗号为分割符)..开始一步一步进行推理，为下一轮行动准备.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " <reflections>\n",
      "第一晚P3杀死P7,这是一个随机行为,没有太多信息可以推断。作为狼人,我需要观察其他人的言行,判断谁最可能是预言家,同时也要注意不暴露自己的身份。下一步我会继续观察大家的发言,看看是否能找到预言家的蛛丝马迹。\n",
      "</reflections>"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:42:38 AM \t\t===== DoAnswer NIGHT 1 P6 ======\n",
      "04/19/2024 03:42:38 AM \tQUESTION: 现在是NIGHT 1,你支持的玩家是P6(狼人身份,本阵营为:P3,P6).目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:存活,P8:存活(逗号为分割符).现在是第1天夜晚，该如何行动?\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " <reflections>\n",
      "第一晚P3杀死P7,这是一个随机行为,没有太多信息可以推断。作为狼人,我需要观察其他人的言行,判断谁最可能是预言家,同时也要注意不暴露自己的身份。下一步我会继续观察大家的发言,看看是否能找到预言家的蛛丝马迹。\n",
      "</reflections>"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:42:43 AM \t\t===== DoAction NIGHT 1 P6 ======\n",
      "04/19/2024 03:42:43 AM \n",
      " [ROUND ACTION]={'time': 'NIGHT 1', 'player': 'P6', 'status': 1, 'role': '狼人', 'character': '过激型', 'response': []}\n",
      "\n",
      "04/19/2024 03:42:43 AM \t===== NIGHT 1 PostAction ======\n",
      "04/19/2024 03:42:43 AM \t [wolf_votes]: ['P7'], [player_vote_name]: Counter({'P7': 1})\n",
      "04/19/2024 03:42:43 AM \n",
      " [WOLF VOTE]={'time': 'NIGHT 1', 'player': 'P7', 'status': 0, 'role': '村民', 'character': '观察家', 'response': '玩家P7于NIGHT 1被狼人投票而出局'}\n",
      "\n",
      "04/19/2024 03:42:43 AM 游戏继续. 目前玩家存活情况:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:淘汰,P8:存活\n",
      "04/19/2024 03:42:43 AM \t===== input_tokens: 17064 output_tokens 357 ======\n",
      "04/19/2024 03:42:43 AM \t===== elapsed_time: 25.683495044708252 ======\n",
      "04/19/2024 03:42:43 AM \t===== DAY 2 PreAction ======\n",
      "04/19/2024 03:42:43 AM \t\t===== DoMemory DAY 2 P7 ======\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 玩家P7在第一轮夜晚被狼人投票淘汰。"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:42:45 AM \t\t===== DoReflect DAY 2 P7 ======\n",
      "04/19/2024 03:42:45 AM \t\t===== DoAnswer DAY 2 P7 ======\n",
      "04/19/2024 03:42:45 AM \tQUESTION: 现在是DAY 2,你支持的玩家是P7(村民身份)..目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:淘汰,P8:存活(逗号为分割符)..开始一步一步进行推理，为下一轮行动准备.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 既然我支持的玩家P7已经被淘汰,那我现在的目标就是帮助剩余的好人阵营获胜。首先需要分析一下目前的游戏局势 - P7作为第一个死亡的玩家,很有可能是被随机投票死的,所以第一晚上所有人的行动都不可信。现在需要观察每个人的言行举止,比如谁在带节奏,谁的言论似是而非,这些都需要提高警惕。我会先观察大家的讨论,然后在下一轮行动时进行怀疑或投票。"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:42:54 AM \t\t===== DoAnswer DAY 2 P7 ======\n",
      "04/19/2024 03:42:54 AM \tQUESTION: 现在是DAY 2,你支持的玩家是P7(村民身份)..目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:淘汰,P8:存活(逗号为分割符).现在是第2天白天，你已经死亡,有何遗言?\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " {\"action\": \"DeathWords\", \"content\": \"我觉得P3之前的言论很可疑,他在混淆视听\"}"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:43:04 AM \t\t===== DoAction DAY 2 P7 ======\n",
      "04/19/2024 03:43:04 AM \n",
      " [ROUND ACTION]={'time': 'DAY 2', 'player': 'P7', 'status': 0, 'role': '村民', 'character': '观察家', 'response': ['{\"action\": \"DeathWords\", \"content\": \"我觉得P3之前的言论很可疑,他在混淆视听\"}']}\n",
      "\n",
      "04/19/2024 03:43:04 AM \t===== DAY 2 DoAction ======\n",
      "04/19/2024 03:43:04 AM \t\t===== DoMemory DAY 2 P1 ======\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 当前场上8名玩家中,P7已被淘汰。P7死前怀疑P3。P1使用预言家技能查验P2为村民。"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:43:18 AM \t\t===== DoReflect DAY 2 P1 ======\n",
      "04/19/2024 03:43:18 AM \t\t===== DoAnswer DAY 2 P1 ======\n",
      "04/19/2024 03:43:18 AM \tQUESTION: 现在是DAY 2,你支持的玩家是P1(预言家身份)..目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:淘汰,P8:存活(逗号为分割符)..开始一步一步进行推理，为下一轮行动准备.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 根据游戏规则,第一个夜晚死亡的一定是村民或者预言家,狼人没必要第一晚上自杀。P7死前怀疑P3,这给我一个提示,P3可能是狼人。我需要继续观察P3的言行,并引导其他玩家关注P3。"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:43:24 AM \t\t===== DoAnswer DAY 2 P1 ======\n",
      "04/19/2024 03:43:24 AM \tQUESTION: 现在是DAY 2,你支持的玩家是P1(预言家身份)..目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:淘汰,P8:存活(逗号为分割符).现在是第2天白天的讨论环节，该如何行动?\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " <reflections>\n",
      "根据游戏规则,第一个夜晚死亡的一定是村民或者预言家,狼人没必要第一晚上自杀。P7死前怀疑P3,这给我一个提示,P3可能是狼人。我需要继续观察P3的言行,并引导其他玩家关注P3。\n",
      "</reflections>"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:43:30 AM \t\t===== DoAction DAY 2 P1 ======\n",
      "04/19/2024 03:43:30 AM \n",
      " [ROUND ACTION]={'time': 'DAY 2', 'player': 'P1', 'status': 1, 'role': '预言家', 'character': '独立思考', 'response': []}\n",
      "\n",
      "04/19/2024 03:43:30 AM \t\t===== DoMemory DAY 2 P2 ======\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 当前场上有8名玩家,P1-P6均存活,P7已被淘汰,P8存活。P7在被淘汰前指出P3之前的言论可疑,似在混淆视听。"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:43:34 AM \t\t===== DoReflect DAY 2 P2 ======\n",
      "04/19/2024 03:43:34 AM \t\t===== DoAnswer DAY 2 P2 ======\n",
      "04/19/2024 03:43:34 AM \tQUESTION: 现在是DAY 2,你支持的玩家是P2(村民身份)..目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:淘汰,P8:存活(逗号为分割符)..开始一步一步进行推理，为下一轮行动准备.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 我先思考一下当前的游戏情况:\n",
      "\n",
      "第一晚上P7被淘汰,根据游戏规则,第一晚上死的一定是好人阵营的村民或预言家。那么P7提供的关于P3可疑言论的信息就很关键。\n",
      "\n",
      "结合P7的死亡遗言,我判断P3很有可能是狼人,在混淆视听。为了确定我的推理,我准备在白天的时候观察P3的言论和举动,进一步判断他的身份。\n",
      "\n",
      "同时我也要注意观察其他人的表现,不能轻易相信所有人,谨防被狼人欺骗。我会继续推理,为下一步的行动做准备。"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:43:44 AM \t\t===== DoAnswer DAY 2 P2 ======\n",
      "04/19/2024 03:43:44 AM \tQUESTION: 现在是DAY 2,你支持的玩家是P2(村民身份)..目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:淘汰,P8:存活(逗号为分割符).现在是第2天白天的讨论环节，该如何行动?\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 根据P7的死亡遗言,我判断P3很可疑,是潜伏的狼人。为了验证我的推理,我需要观察P3这一天的言行。同时也要留意其他人的表现,不能完全相信所有人。我的行动是:\n",
      "\n",
      "{\"action\": \"Debate\", \"content\": \"我觉得P3今天的言论和举动很可疑,他可能是狼人,我会继续观察他的表现。\"}"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:43:50 AM \t\t===== DoAction DAY 2 P2 ======\n",
      "04/19/2024 03:43:50 AM \n",
      " [ROUND ACTION]={'time': 'DAY 2', 'player': 'P2', 'status': 1, 'role': '村民', 'character': '腼腆型', 'response': ['{\"action\": \"Debate\", \"content\": \"我觉得P3今天的言论和举动很可疑,他可能是狼人,我会继续观察他的表现。\"}']}\n",
      "\n",
      "04/19/2024 03:43:50 AM \t\t===== DoMemory DAY 2 P3 ======\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 当前场上有8名玩家,P7已被投票淘汰。P7死亡遗言怀疑P3。P2也怀疑P3的言行可疑,可能是狼人。夜晚阶段P3作为狼人投票淘汰了P7。"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:43:54 AM \t\t===== DoReflect DAY 2 P3 ======\n",
      "04/19/2024 03:43:54 AM \t\t===== DoAnswer DAY 2 P3 ======\n",
      "04/19/2024 03:43:54 AM \tQUESTION: 现在是DAY 2,你支持的玩家是P3(狼人身份,本阵营为:P3,P6).目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:淘汰,P8:存活(逗号为分割符)..开始一步一步进行推理，为下一轮行动准备.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 既然P7死亡遗言怀疑我P3,那我需要先进行辩解。我的思考是:\n",
      "\n",
      "{\"action\": \"Debate\", \"content\": \"我不是狼人,P7死亡遗言纯属无端诬陷。\"}\n",
      "\n",
      "接下来还需要引导其他人的注意力,转移他们的视线。我建议:\n",
      "\n",
      "{\"action\": \"PlayerDoubt\", \"target\": \"P2\"} \n",
      "\n",
      "通过怀疑P2,可以转移其他人对我的注意力,同时也能观察P2的反应,看他是否紧张。"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "04/19/2024 03:44:21 AM \t\t===== DoAnswer DAY 2 P3 ======\n",
      "04/19/2024 03:44:21 AM \tQUESTION: 现在是DAY 2,你支持的玩家是P3(狼人身份,本阵营为:P3,P6).目前场上玩家:P1:存活,P2:存活,P3:存活,P4:存活,P5:存活,P6:存活,P7:淘汰,P8:存活(逗号为分割符).现在是第2天白天的讨论环节，该如何行动?\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " {\"action\": \"Debate\", \"content\": \"我不是狼人,P7的死亡遗言纯属"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 9\u001b[0m\n\u001b[1;32m      7\u001b[0m GM \u001b[38;5;241m=\u001b[39m GameMaster(\u001b[38;5;241m10\u001b[39m, llm, \u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m      8\u001b[0m GM\u001b[38;5;241m.\u001b[39mResetGame()\n\u001b[0;32m----> 9\u001b[0m \u001b[43mGM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mRunGame\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     10\u001b[0m GM\u001b[38;5;241m.\u001b[39mEndGame()\n",
      "File \u001b[0;32m~/SageMaker/LLM-Game-Agents/WereWolf/utils/GameMaster.py:406\u001b[0m, in \u001b[0;36mGameMaster.RunGame\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    398\u001b[0m \u001b[38;5;66;03m# messages = []\u001b[39;00m\n\u001b[1;32m    399\u001b[0m \u001b[38;5;66;03m# while not self.game_memory_queue.empty():\u001b[39;00m\n\u001b[1;32m    400\u001b[0m \u001b[38;5;66;03m#     messages.append(self.game_memory_queue.get(False))    \u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    403\u001b[0m \u001b[38;5;66;03m#     question = game_config_dict[\"god\"][\"action_plan_death\"].format(\",\".join(messages), i)\u001b[39;00m\n\u001b[1;32m    404\u001b[0m \u001b[38;5;66;03m# self.god_instruct = self._invoke(question)[\"response\"]\u001b[39;00m\n\u001b[1;32m    405\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mPreAction(i)\n\u001b[0;32m--> 406\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mDoAction\u001b[49m\u001b[43m(\u001b[49m\u001b[43mi\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    407\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mPostAction(i)\n\u001b[1;32m    408\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mwinner \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n",
      "File \u001b[0;32m~/SageMaker/LLM-Game-Agents/WereWolf/utils/GameMaster.py:276\u001b[0m, in \u001b[0;36mGameMaster.DoAction\u001b[0;34m(self, i)\u001b[0m\n\u001b[1;32m    274\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m player\u001b[38;5;241m.\u001b[39mGetStatus() \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m: \n\u001b[1;32m    275\u001b[0m     question_template \u001b[38;5;241m=\u001b[39m game_config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mplayer\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124maction_plan_day\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[0;32m--> 276\u001b[0m     \u001b[43mplayer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mDoPlanning\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquestion_template\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    277\u001b[0m     \u001b[38;5;28;01mpass\u001b[39;00m\n\u001b[1;32m    278\u001b[0m \u001b[38;5;28;01mpass\u001b[39;00m\n",
      "File \u001b[0;32m~/SageMaker/LLM-Game-Agents/WereWolf/utils/GamePlayer.py:103\u001b[0m, in \u001b[0;36mGamePlayer.DoPlanning\u001b[0;34m(self, question_template, idx)\u001b[0m\n\u001b[1;32m    100\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mGM\u001b[38;5;241m.\u001b[39mgod_instruct \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m    101\u001b[0m     question \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_playerInfoBuilder() \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mGM\u001b[38;5;241m.\u001b[39mgod_instruct\n\u001b[0;32m--> 103\u001b[0m answerapi \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mDoAnswer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquestion\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    104\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mDoAction(answerapi)\n\u001b[1;32m    105\u001b[0m \u001b[38;5;28;01mpass\u001b[39;00m\n",
      "File \u001b[0;32m~/SageMaker/LLM-Game-Agents/WereWolf/utils/GamePlayer.py:110\u001b[0m, in \u001b[0;36mGamePlayer.DoAnswer\u001b[0;34m(self, question)\u001b[0m\n\u001b[1;32m    108\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mDoAnswer\u001b[39m(\u001b[38;5;28mself\u001b[39m, question):\n\u001b[1;32m    109\u001b[0m     Info(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[38;5;124m===== DoAnswer \u001b[39m\u001b[38;5;132;01m{0}\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;132;01m{1}\u001b[39;00m\u001b[38;5;124m ======\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mGM\u001b[38;5;241m.\u001b[39mcurrent_time,\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39magent[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m\"\u001b[39m]))\n\u001b[0;32m--> 110\u001b[0m     answer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_invoke\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquestion\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    111\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m answer\n",
      "File \u001b[0;32m~/SageMaker/LLM-Game-Agents/WereWolf/utils/GamePlayer.py:49\u001b[0m, in \u001b[0;36mGamePlayer._invoke\u001b[0;34m(self, question)\u001b[0m\n\u001b[1;32m     47\u001b[0m Info(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[38;5;124mQUESTION: \u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m question)\n\u001b[1;32m     48\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m500\u001b[39m \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m1000\u001b[39m)\n\u001b[0;32m---> 49\u001b[0m answer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43magent\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mconversation\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mquestion\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcallbacks\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtoken_counter\u001b[49m\u001b[43m]\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     50\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mGM\u001b[38;5;241m.\u001b[39minput_tokens \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mGM\u001b[38;5;241m.\u001b[39minput_tokens \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtoken_counter\u001b[38;5;241m.\u001b[39minput_tokens\n\u001b[1;32m     51\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mGM\u001b[38;5;241m.\u001b[39moutput_tokens \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mGM\u001b[38;5;241m.\u001b[39moutput_tokens \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtoken_counter\u001b[38;5;241m.\u001b[39moutput_tokens\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain/chains/base.py:163\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m    161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    162\u001b[0m     run_manager\u001b[38;5;241m.\u001b[39mon_chain_error(e)\n\u001b[0;32m--> 163\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m e\n\u001b[1;32m    164\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_end(outputs)\n\u001b[1;32m    166\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m include_run_info:\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain/chains/base.py:153\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m    150\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m    151\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_inputs(inputs)\n\u001b[1;32m    152\u001b[0m     outputs \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m--> 153\u001b[0m         \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    154\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n\u001b[1;32m    155\u001b[0m         \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call(inputs)\n\u001b[1;32m    156\u001b[0m     )\n\u001b[1;32m    158\u001b[0m     final_outputs: Dict[\u001b[38;5;28mstr\u001b[39m, Any] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprep_outputs(\n\u001b[1;32m    159\u001b[0m         inputs, outputs, return_only_outputs\n\u001b[1;32m    160\u001b[0m     )\n\u001b[1;32m    161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain/chains/llm.py:103\u001b[0m, in \u001b[0;36mLLMChain._call\u001b[0;34m(self, inputs, run_manager)\u001b[0m\n\u001b[1;32m     98\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_call\u001b[39m(\n\u001b[1;32m     99\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m    100\u001b[0m     inputs: Dict[\u001b[38;5;28mstr\u001b[39m, Any],\n\u001b[1;32m    101\u001b[0m     run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m    102\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Dict[\u001b[38;5;28mstr\u001b[39m, \u001b[38;5;28mstr\u001b[39m]:\n\u001b[0;32m--> 103\u001b[0m     response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgenerate\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    104\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcreate_outputs(response)[\u001b[38;5;241m0\u001b[39m]\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain/chains/llm.py:115\u001b[0m, in \u001b[0;36mLLMChain.generate\u001b[0;34m(self, input_list, run_manager)\u001b[0m\n\u001b[1;32m    113\u001b[0m callbacks \u001b[38;5;241m=\u001b[39m run_manager\u001b[38;5;241m.\u001b[39mget_child() \u001b[38;5;28;01mif\u001b[39;00m run_manager \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    114\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mllm, BaseLanguageModel):\n\u001b[0;32m--> 115\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mllm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgenerate_prompt\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    116\u001b[0m \u001b[43m        \u001b[49m\u001b[43mprompts\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    117\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    118\u001b[0m \u001b[43m        \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    119\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mllm_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    120\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    121\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    122\u001b[0m     results \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mllm\u001b[38;5;241m.\u001b[39mbind(stop\u001b[38;5;241m=\u001b[39mstop, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mllm_kwargs)\u001b[38;5;241m.\u001b[39mbatch(\n\u001b[1;32m    123\u001b[0m         cast(List, prompts), {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcallbacks\u001b[39m\u001b[38;5;124m\"\u001b[39m: callbacks}\n\u001b[1;32m    124\u001b[0m     )\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_core/language_models/llms.py:597\u001b[0m, in \u001b[0;36mBaseLLM.generate_prompt\u001b[0;34m(self, prompts, stop, callbacks, **kwargs)\u001b[0m\n\u001b[1;32m    589\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mgenerate_prompt\u001b[39m(\n\u001b[1;32m    590\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m    591\u001b[0m     prompts: List[PromptValue],\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    594\u001b[0m     \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Any,\n\u001b[1;32m    595\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m LLMResult:\n\u001b[1;32m    596\u001b[0m     prompt_strings \u001b[38;5;241m=\u001b[39m [p\u001b[38;5;241m.\u001b[39mto_string() \u001b[38;5;28;01mfor\u001b[39;00m p \u001b[38;5;129;01min\u001b[39;00m prompts]\n\u001b[0;32m--> 597\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgenerate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprompt_strings\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_core/language_models/llms.py:767\u001b[0m, in \u001b[0;36mBaseLLM.generate\u001b[0;34m(self, prompts, stop, callbacks, tags, metadata, run_name, run_id, **kwargs)\u001b[0m\n\u001b[1;32m    752\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcache \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m get_llm_cache() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcache \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m:\n\u001b[1;32m    753\u001b[0m     run_managers \u001b[38;5;241m=\u001b[39m [\n\u001b[1;32m    754\u001b[0m         callback_manager\u001b[38;5;241m.\u001b[39mon_llm_start(\n\u001b[1;32m    755\u001b[0m             dumpd(\u001b[38;5;28mself\u001b[39m),\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    765\u001b[0m         )\n\u001b[1;32m    766\u001b[0m     ]\n\u001b[0;32m--> 767\u001b[0m     output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_generate_helper\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    768\u001b[0m \u001b[43m        \u001b[49m\u001b[43mprompts\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_managers\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mbool\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mnew_arg_supported\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\n\u001b[1;32m    769\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    770\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m output\n\u001b[1;32m    771\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(missing_prompts) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_core/language_models/llms.py:634\u001b[0m, in \u001b[0;36mBaseLLM._generate_helper\u001b[0;34m(self, prompts, stop, run_managers, new_arg_supported, **kwargs)\u001b[0m\n\u001b[1;32m    632\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m run_manager \u001b[38;5;129;01min\u001b[39;00m run_managers:\n\u001b[1;32m    633\u001b[0m         run_manager\u001b[38;5;241m.\u001b[39mon_llm_error(e, response\u001b[38;5;241m=\u001b[39mLLMResult(generations\u001b[38;5;241m=\u001b[39m[]))\n\u001b[0;32m--> 634\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m e\n\u001b[1;32m    635\u001b[0m flattened_outputs \u001b[38;5;241m=\u001b[39m output\u001b[38;5;241m.\u001b[39mflatten()\n\u001b[1;32m    636\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m manager, flattened_output \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(run_managers, flattened_outputs):\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_core/language_models/llms.py:621\u001b[0m, in \u001b[0;36mBaseLLM._generate_helper\u001b[0;34m(self, prompts, stop, run_managers, new_arg_supported, **kwargs)\u001b[0m\n\u001b[1;32m    611\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_generate_helper\u001b[39m(\n\u001b[1;32m    612\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m    613\u001b[0m     prompts: List[\u001b[38;5;28mstr\u001b[39m],\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    617\u001b[0m     \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Any,\n\u001b[1;32m    618\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m LLMResult:\n\u001b[1;32m    619\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m    620\u001b[0m         output \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m--> 621\u001b[0m             \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_generate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    622\u001b[0m \u001b[43m                \u001b[49m\u001b[43mprompts\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    623\u001b[0m \u001b[43m                \u001b[49m\u001b[43mstop\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    624\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;66;43;03m# TODO: support multiple run managers\u001b[39;49;00m\n\u001b[1;32m    625\u001b[0m \u001b[43m                \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_managers\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mrun_managers\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m    626\u001b[0m \u001b[43m                \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    627\u001b[0m \u001b[43m            \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    628\u001b[0m             \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n\u001b[1;32m    629\u001b[0m             \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_generate(prompts, stop\u001b[38;5;241m=\u001b[39mstop)\n\u001b[1;32m    630\u001b[0m         )\n\u001b[1;32m    631\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    632\u001b[0m         \u001b[38;5;28;01mfor\u001b[39;00m run_manager \u001b[38;5;129;01min\u001b[39;00m run_managers:\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_core/language_models/llms.py:1231\u001b[0m, in \u001b[0;36mLLM._generate\u001b[0;34m(self, prompts, stop, run_manager, **kwargs)\u001b[0m\n\u001b[1;32m   1228\u001b[0m new_arg_supported \u001b[38;5;241m=\u001b[39m inspect\u001b[38;5;241m.\u001b[39msignature(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call)\u001b[38;5;241m.\u001b[39mparameters\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrun_manager\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m   1229\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m prompt \u001b[38;5;129;01min\u001b[39;00m prompts:\n\u001b[1;32m   1230\u001b[0m     text \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m-> 1231\u001b[0m         \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprompt\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1232\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n\u001b[1;32m   1233\u001b[0m         \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call(prompt, stop\u001b[38;5;241m=\u001b[39mstop, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m   1234\u001b[0m     )\n\u001b[1;32m   1235\u001b[0m     generations\u001b[38;5;241m.\u001b[39mappend([Generation(text\u001b[38;5;241m=\u001b[39mtext)])\n\u001b[1;32m   1236\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m LLMResult(generations\u001b[38;5;241m=\u001b[39mgenerations)\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_community/llms/bedrock.py:833\u001b[0m, in \u001b[0;36mBedrock._call\u001b[0;34m(self, prompt, stop, run_manager, **kwargs)\u001b[0m\n\u001b[1;32m    831\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstreaming:\n\u001b[1;32m    832\u001b[0m     completion \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m--> 833\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m chunk \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_stream(\n\u001b[1;32m    834\u001b[0m         prompt\u001b[38;5;241m=\u001b[39mprompt, stop\u001b[38;5;241m=\u001b[39mstop, run_manager\u001b[38;5;241m=\u001b[39mrun_manager, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[1;32m    835\u001b[0m     ):\n\u001b[1;32m    836\u001b[0m         completion \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m chunk\u001b[38;5;241m.\u001b[39mtext\n\u001b[1;32m    837\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m completion\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_community/llms/bedrock.py:656\u001b[0m, in \u001b[0;36mBedrockBase._prepare_input_and_invoke_stream\u001b[0;34m(self, prompt, system, messages, stop, run_manager, **kwargs)\u001b[0m\n\u001b[1;32m    653\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    654\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError raised by bedrock service: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00me\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 656\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m chunk \u001b[38;5;129;01min\u001b[39;00m LLMInputOutputAdapter\u001b[38;5;241m.\u001b[39mprepare_output_stream(\n\u001b[1;32m    657\u001b[0m     provider, response, stop, \u001b[38;5;28;01mTrue\u001b[39;00m \u001b[38;5;28;01mif\u001b[39;00m messages \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[1;32m    658\u001b[0m ):\n\u001b[1;32m    659\u001b[0m     \u001b[38;5;28;01myield\u001b[39;00m chunk\n\u001b[1;32m    660\u001b[0m     \u001b[38;5;66;03m# verify and raise callback error if any middleware intervened\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/langchain_community/llms/bedrock.py:203\u001b[0m, in \u001b[0;36mLLMInputOutputAdapter.prepare_output_stream\u001b[0;34m(cls, provider, response, stop, messages_api)\u001b[0m\n\u001b[1;32m    198\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m output_key:\n\u001b[1;32m    199\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m    200\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnknown streaming response output key for provider: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mprovider\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    201\u001b[0m     )\n\u001b[0;32m--> 203\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m event \u001b[38;5;129;01min\u001b[39;00m stream:\n\u001b[1;32m    204\u001b[0m     chunk \u001b[38;5;241m=\u001b[39m event\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mchunk\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m    205\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m chunk:\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/botocore/eventstream.py:602\u001b[0m, in \u001b[0;36mEventStream.__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    601\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__iter__\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[0;32m--> 602\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m event \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_event_generator:\n\u001b[1;32m    603\u001b[0m         parsed_event \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_parse_event(event)\n\u001b[1;32m    604\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m parsed_event:\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/botocore/eventstream.py:609\u001b[0m, in \u001b[0;36mEventStream._create_raw_event_generator\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    607\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_create_raw_event_generator\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m    608\u001b[0m     event_stream_buffer \u001b[38;5;241m=\u001b[39m EventStreamBuffer()\n\u001b[0;32m--> 609\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m chunk \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_raw_stream\u001b[38;5;241m.\u001b[39mstream():\n\u001b[1;32m    610\u001b[0m         event_stream_buffer\u001b[38;5;241m.\u001b[39madd_data(chunk)\n\u001b[1;32m    611\u001b[0m         \u001b[38;5;28;01myield from\u001b[39;00m event_stream_buffer\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/urllib3/response.py:933\u001b[0m, in \u001b[0;36mHTTPResponse.stream\u001b[0;34m(self, amt, decode_content)\u001b[0m\n\u001b[1;32m    917\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    918\u001b[0m \u001b[38;5;124;03mA generator wrapper for the read() method. A call will block until\u001b[39;00m\n\u001b[1;32m    919\u001b[0m \u001b[38;5;124;03m``amt`` bytes have been read from the connection or until the\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    930\u001b[0m \u001b[38;5;124;03m    'content-encoding' header.\u001b[39;00m\n\u001b[1;32m    931\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    932\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchunked \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msupports_chunked_reads():\n\u001b[0;32m--> 933\u001b[0m     \u001b[38;5;28;01myield from\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mread_chunked(amt, decode_content\u001b[38;5;241m=\u001b[39mdecode_content)\n\u001b[1;32m    934\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    935\u001b[0m     \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_fp_closed(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_fp) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_decoded_buffer) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/urllib3/response.py:1073\u001b[0m, in \u001b[0;36mHTTPResponse.read_chunked\u001b[0;34m(self, amt, decode_content)\u001b[0m\n\u001b[1;32m   1070\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m   1072\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m-> 1073\u001b[0m     \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_update_chunk_length\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1074\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchunk_left \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m   1075\u001b[0m         \u001b[38;5;28;01mbreak\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/urllib3/response.py:1001\u001b[0m, in \u001b[0;36mHTTPResponse._update_chunk_length\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    999\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchunk_left \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m   1000\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m-> 1001\u001b[0m line \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_fp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreadline\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# type: ignore[union-attr]\u001b[39;00m\n\u001b[1;32m   1002\u001b[0m line \u001b[38;5;241m=\u001b[39m line\u001b[38;5;241m.\u001b[39msplit(\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m;\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m1\u001b[39m)[\u001b[38;5;241m0\u001b[39m]\n\u001b[1;32m   1003\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/socket.py:705\u001b[0m, in \u001b[0;36mSocketIO.readinto\u001b[0;34m(self, b)\u001b[0m\n\u001b[1;32m    703\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m    704\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 705\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_sock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecv_into\u001b[49m\u001b[43m(\u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    706\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m timeout:\n\u001b[1;32m    707\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_timeout_occurred \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/ssl.py:1307\u001b[0m, in \u001b[0;36mSSLSocket.recv_into\u001b[0;34m(self, buffer, nbytes, flags)\u001b[0m\n\u001b[1;32m   1303\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m flags \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m   1304\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   1305\u001b[0m           \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnon-zero flags not allowed in calls to recv_into() on \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m\n\u001b[1;32m   1306\u001b[0m           \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m)\n\u001b[0;32m-> 1307\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnbytes\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbuffer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1308\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m   1309\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39mrecv_into(buffer, nbytes, flags)\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch_p310/lib/python3.10/ssl.py:1163\u001b[0m, in \u001b[0;36mSSLSocket.read\u001b[0;34m(self, len, buffer)\u001b[0m\n\u001b[1;32m   1161\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m   1162\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m buffer \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1163\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_sslobj\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbuffer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1164\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m   1165\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_sslobj\u001b[38;5;241m.\u001b[39mread(\u001b[38;5;28mlen\u001b[39m)\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "from utils.GamePlayer import GamePlayer\n",
    "from utils.GameMaster import GameMaster\n",
    "from utils.PeTemplates import *\n",
    "from utils import ParseJson, print_ww, Print, Info, Debug, Warn, Error\n",
    "\n",
    "llm = claude_llm\n",
    "GM = GameMaster(10, llm, False)\n",
    "GM.ResetGame()\n",
    "GM.RunGame()\n",
    "GM.EndGame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adfcc21a-1ecf-4f3b-b73e-8d74baadb46e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_pytorch_p310",
   "language": "python",
   "name": "conda_pytorch_p310"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
